﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Schism.Core
{
    [Serializable]
    public class DungeonPos : IEquatable<DungeonPos>
    {
        //Fields
        private Vector2 _worldPosition;

        public Vector2 WorldPosition
        {
            get { return _worldPosition; }
            set { _worldPosition = value; }
        }
        public Vector2 MapPosition
        {
            get { return new Vector2(_worldPosition.X / Globals.GLOBAL_TILE_WIDTH, _worldPosition.Y / Globals.GLOBAL_TILE_HEIGHT); }
            set
            {
                _worldPosition = new Vector2(value.X * Globals.GLOBAL_TILE_WIDTH, value.Y * Globals.GLOBAL_TILE_HEIGHT);
            }
        }
        public Point MapPositionPoint
        {
            get { return new Point((int)(_worldPosition.X / Globals.GLOBAL_TILE_WIDTH), (int)(_worldPosition.Y / Globals.GLOBAL_TILE_HEIGHT)); }
            set
            {
                _worldPosition = new Vector2(value.X * Globals.GLOBAL_TILE_WIDTH, value.Y * Globals.GLOBAL_TILE_HEIGHT);
            }
        }
        public Vector2 CenterTile
        {
            get
            {
                return new Vector2(
                    MapPosition.X + Globals.GLOBAL_TILE_WIDTH,
                    MapPosition.Y + Globals.GLOBAL_TILE_HEIGHT);
            }
        }

        public float X { get { return WorldPosition.X; } set { WorldPosition = new Vector2(value, WorldPosition.Y); } }
        public float Y { get { return WorldPosition.Y; } set { WorldPosition = new Vector2(WorldPosition.X, value); } }

        public int MapX { get { return MapPositionPoint.X; } set { MapPositionPoint = new Point(value, MapPositionPoint.Y); } }
        public int MapY { get { return MapPositionPoint.Y; } set { MapPositionPoint = new Point(MapPositionPoint.X, value); } }

        //ctor
        private DungeonPos() { }
        public DungeonPos(Vector2 worldPosition)
        {
            this.WorldPosition = worldPosition;
        }
        public DungeonPos(Point mapPosition)
        {
            this.MapPositionPoint = mapPosition;
        }
        public DungeonPos(int x, int y)
        {
            this.MapPositionPoint = new Point(x,y);
        }

        //Methods
        public static DungeonPos operator +(DungeonPos a, DungeonPos b)
        {
            return new DungeonPos(a.WorldPosition + b.WorldPosition);
        }
        public static DungeonPos operator -(DungeonPos a, DungeonPos b)
        {
            return  new DungeonPos(a.WorldPosition - b.WorldPosition);
        }
        public static DungeonPos operator *(DungeonPos a, DungeonPos b)
        {
            return new DungeonPos(a.WorldPosition * b.WorldPosition);
        }
        public static DungeonPos operator /(DungeonPos a, DungeonPos b)
        {
            return new DungeonPos(a.WorldPosition / b.WorldPosition);
        }

        public static DungeonPos CreateFromMapPosition(Point mapPosition)
        {
            return new DungeonPos() { MapPositionPoint = mapPosition };
        }

        public bool Equals(DungeonPos other)
        {
            return other.WorldPosition == this.WorldPosition;
        }
    }
}
